package com.yh.csx.bsf.flow.dao.dal;

import com.yh.csx.bsf.flow.core.db.DbConn;
import com.yh.csx.bsf.flow.core.util.ArrayUtil;
import com.yh.csx.bsf.flow.dao.dal.auto.t_department_base_dal;
import com.yh.csx.bsf.flow.dao.model.auto.t_department_model;
import lombok.val;
import lombok.var;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class t_department_dal extends t_department_base_dal {
    public t_department_model getFirstDepartment(DbConn conn,String orgName){
        val par = new Object[]{orgName};
        val stringSql = new StringBuilder();
        stringSql.append("select s.* from t_department s where s.name=? and level=1");
        val ds = conn.executeList(stringSql.toString(), par);
        if (ds != null && ds.size() > 0) {
            return createModel(ds.get(0));
        }
        return null;
    }
    public ArrayList<t_department_model> list(DbConn conn,List<Integer> ids) {
        val rs = new ArrayList<t_department_model>();
        val stringSql = new StringBuilder();
        stringSql.append("select s.* from t_department s where id in ("+ArrayUtil.sqlIn(ids)+")");
        val ds = conn.executeList(stringSql.toString(), new Object[]{});
        if (ds != null && ds.size() > 0) {
            for (Map<String, Object> dr : ds) {
                rs.add(createModel(dr));
            }
        }
        return rs;
    }

    public ArrayList<t_department_model> listByParPath(DbConn conn,List<String> parPaths,List<Integer> levels) {
        val rs = new ArrayList<t_department_model>();
        val whereSql = new StringBuilder(" where 1=1 ");
        val params = new ArrayList<>();
        var index=0;
        for(val path:parPaths){
            whereSql.append(" or (par_path like concat('%', ?, '%') and level=?) ");
            params.add(path);
            params.add(levels.get(index));
            index++;
        }
        val stringSql = new StringBuilder();
        stringSql.append("select s.* from t_department s "+whereSql.toString());
        val ds = conn.executeList(stringSql.toString(), params.toArray());
        if (ds != null && ds.size() > 0) {
            for (Map<String, Object> dr : ds) {
                rs.add(createModel(dr));
            }
        }
        return rs;
    }

    public t_department_model getDepartment(DbConn conn,Integer firstId,String name){
        val par = new Object[]{firstId,name};
        val stringSql = new StringBuilder();
        stringSql.append("select s.* from t_department s where first_par_id=? and s.name=?");
        val ds = conn.executeList(stringSql.toString(), par);
        if (ds != null && ds.size() > 0) {
            return createModel(ds.get(0));
        }
        return null;
    }
}
